package com.flxz.user.service.impl;

import com.flxz.user.domain.UserExpense;
import com.flxz.user.domain.UserIncome;
import com.flxz.user.mapper.UserExpenseMapper;
import com.flxz.user.mapper.UserIncomeMapper;
import com.flxz.user.service.IUserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户记录Service业务层处理
 */
@Service
public class UserRecordServiceImpl implements IUserRecordService {

    @Autowired
    private UserIncomeMapper userIncomeMapper;

    @Autowired
    private UserExpenseMapper userExpenseMapper;

    @Override
    public Map<String, Object> getRecords(Long userId, Integer page, Integer pageSize, String type, String startDate, String endDate) {
        List<Map<String, Object>> records = new ArrayList<>();
        int total = 0;

        // 查询收入记录
        if (type == null || "income".equals(type)) {
            UserIncome incomeQuery = new UserIncome();
            incomeQuery.setUserId(userId);
            List<UserIncome> incomeList = userIncomeMapper.selectUserIncomeList(incomeQuery);
            
            for (UserIncome income : incomeList) {
                Map<String, Object> record = new HashMap<>();
                record.put("id", income.getIncomeId());
                record.put("type", "income");
                record.put("amount", income.getAmount());
                record.put("date", income.getIncomeTime());
                record.put("categoryId", income.getCategoryId());
                record.put("VOclass", getCategoryName(income.getCategoryId(), "income"));
                record.put("remark", income.getRemark());
                records.add(record);
            }
        }

        // 查询支出记录
        if (type == null || "expense".equals(type)) {
            UserExpense expenseQuery = new UserExpense();
            expenseQuery.setUserId(userId);
            List<UserExpense> expenseList = userExpenseMapper.selectUserExpenseList(expenseQuery);
            
            for (UserExpense expense : expenseList) {
                Map<String, Object> record = new HashMap<>();
                record.put("id", expense.getExpenseId());
                record.put("type", "expense");
                record.put("amount", expense.getAmount());
                record.put("date", expense.getExpenseTime());
                record.put("categoryId", expense.getCategoryId());
                record.put("VOclass", getCategoryName(expense.getCategoryId(), "expense"));
                record.put("remark", expense.getRemark());
                records.add(record);
            }
        }

        // 按日期排序
        records.sort((a, b) -> {
            Date dateA = (Date) a.get("date");
            Date dateB = (Date) b.get("date");
            return dateB.compareTo(dateA); // 降序
        });

        total = records.size();

        // 分页处理
        int start = (page - 1) * pageSize;
        int end = Math.min(start + pageSize, records.size());
        List<Map<String, Object>> pagedRecords = records.subList(start, end);

        Map<String, Object> result = new HashMap<>();
        result.put("records", pagedRecords);
        result.put("total", total);
        
        return result;
    }

    private String getCategoryName(Long categoryId, String type) {
        // 这里可以根据 categoryId 和 type 返回对应的分类名称
        // 暂时返回简单的映射
        if ("income".equals(type)) {
            switch (categoryId.intValue()) {
                case 1: return "工资";
                case 2: return "奖金";
                case 3: return "投资收益";
                case 4: return "意外之财";
                case 5: return "其他收入";
                default: return "未知收入";
            }
        } else {
            switch (categoryId.intValue()) {
                case 1: return "餐饮";
                case 2: return "交通";
                case 3: return "购物";
                case 4: return "娱乐";
                case 5: return "居住";
                case 6: return "医疗";
                case 7: return "教育";
                case 8: return "其他支出";
                default: return "未知支出";
            }
        }
    }
}
